﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FormView.UI.PromptView
{
	public class PromptCheckBoxView : IPromptViewControl
	{
		public static readonly string TagName = "checkbox";

		public object GetValue(FrameworkElement element)
		{
			Grid grid = element as Grid;
			CheckBox cbx = grid.Children[0] as CheckBox;
			return cbx.IsChecked;
		}

		public FrameworkElement GetElement(PromptFormWindow win, string tipText, object value)
		{
			bool defaultVal = false;
			if (value != null)
			{
				if (!bool.TryParse(value.ToString(), out defaultVal))
				{
					throw new ArgumentException("Prompt Checkbox组件的默认值必须为true/false");
				}
			}

			Grid grid = new Grid();
			grid.Background = PromptFormWindow.DefaultBgColor;
			grid.RowDefinitions.Add(new RowDefinition());
			grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star) });

			CheckBox cbx = new CheckBox()
			{
				Padding = new Thickness(2),
				Foreground = PromptFormWindow.DefaultForeColor,
				Background = new SolidColorBrush(PromptFormWindow.ParseColorStr("#33000000")),
				Content = tipText,
				Style = win.Resources["CheckboxStyle"] as Style,
				IsChecked = defaultVal
			};
			Grid.SetColumn(cbx, 0);
			grid.Children.Add(cbx);

			return grid;
		}

		public void OnRenderView(PromptFormWindow promptWindow, FrameworkElement element)
		{
			// ignore
		}
	}
}
